<template>
	<view>
		<view class="returnbox">
			<view class="" >
				<view class="u-m-b-30">请添加图片 <text style="color: red;">(代送退换件包裹请上传拍照)</text> </view>
				<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic"  name="1" multiple :maxCount="3">
					<image src="../../static/uploadimg.jpg" mode="widthFix"
						style="width: 286rpx;"></image>
				</u-upload>
			</view>
			<view class="u-m-t-100"  v-for="(item,index) in orderlist ">
				<view class="flex" style="justify-content: space-between;">
					<text>快递信息</text>
					<u-icon @click="mydelete(index)" name='close'></u-icon>
				</view>
				<view class="inputbox u-m-t-60">
					<view class="flex">
						<text>取件地址：</text>
						<input type="text" placeholder="请输入取件地址" v-model="item.yztitle" />
					</view>
					<u-line margin='20rpx auto 0'></u-line>
				</view>
				<view class="inputbox u-m-t-40">
					<view class="flex">
						<text>快递商家：</text>
						<input type="text" placeholder="请输入快递商家" v-model="item.business" />
					</view>
					<u-line margin='20rpx auto 0'></u-line>
				</view>
				<view class="inputbox u-m-t-40">
					<view class="flex">
						<text>备注信息：</text>
						<input type="text" placeholder="请输入备注信息" v-model="item.remark" />
					</view>
					<u-line margin='20rpx auto 0'></u-line>
				</view>
				<view class="myinputbox">
					<text>选择规格：</text>
					<uni-section title="选择快递类型" type="">
						<uni-data-select v-model="item.type" :localdata="range" @change="change"></uni-data-select>
					</uni-section>
				</view>
			</view>
		</view>
		<view class="estimate">
			<!-- 下单时多多驿站寄件或者上门取件须下单到下午16点后的订单，必须在下午15点前点前放置楼下并进行拍照 -->
			<view class="estimatebox">
				<view class="estimate_left">
					<view class="">
						<text>预估总价</text>
						<text style="color: red;">￥{{allprice}}({{allprice}}金币)</text>
					</view>
					<text class="grey main-text u-m-t-10">{{choosespecification}}</text>
				</view>
				<view class="makesure" @click="showpop">
					<text>确认下单</text>
				</view>
			</view>
		</view>
		<u-popup :show="show" @close="close" bgColor='#f4f4f4' round=20 :closeOnClickOverlay='true' @open="open">
			<orderpop @showclick='showclick' :allprice='allprice' @closeorder='closeorder' :ordertype='1' :orderlist='orderlist' ></orderpop>
		</u-popup>
		<u-popup :show="childrenshow" @close="childrenclose" round=10 :closeOnClickOverlay='true' @open="childrenopen">
			<infopop @cancelclick='cancelclick' @confirmclick='confirmclick'>
			</infopop>
		</u-popup>
		
		<view class="plus" @click="addlist">
			<u-icon size=38 color='#315ee6' name='plus-circle'></u-icon>
		</view>
	</view>
</template>

<script>
	import orderpop from '../../components/orderpop.vue';
	import infopop from '../../components/infopop.vue';
	export default {
		components:{
			orderpop,
			infopop
		},
		data() {
			return {
				fileList1: [],
				address:'',
				orderlist:[{yztitle:'',business:'',type:0,remark:''}],
				
				childrenshow:false,
				show:false,		
				
				
				range: [{
						value: "大件",
						text: "大件"
					},
					{
						value: "中件",
						text: "中件"
					},
					{
						value: "小件",
						text: "小件"
					},
					{
						value: "超大件",
						text: "超大件"
					},
				],
				choosespecification:'',
				allprice:0.00,
				myimg:[]
			};
		},
		methods:{
			//获取价格
			getmoney(){
				this.$api.post('/Delivery/getPickuppackmoney',{
					bejsarr:this.orderlist,
					op:2
				}).then(res=>{
					this.allprice = res.data
				})
			},
			close(){
				this.show = false
			},
			open(){
				
			},
			mydelete(index){
				if(this.orderlist.length==1){
					this.$toast('至少填写一个快递信息~')
				}else{
					this.orderlist.splice(index,1)
				}
			},
			closeorder(e){
				this.show = e
			},
			addlist(){
				this.orderlist.push({yztitle:'',business:'',type:0,remark:'',images:this.myimg.join(',')})
				this.$toast('添加成功~')
				console.log(this.orderlist);
			},
			childrenopen(){
				
			},
			cancelclick(e){
				this.childrenshow = e
			},
			confirmclick(e){
				this.childrenshow = e
			},
			
			//组件传来的值
			showclick(e){
				this.childrenshow = e
			},
			change(e) {
				this.getmoney()
				this.range.map(item => {
					if (e == item.value) {
						
					}
				})
			},
			showpop() {
				this.orderlist.map((item,index)=>{
					if(!item.type){
						this.$toast(`请选择第${index+1}个的包裹规格~`)
					}
					if(!item.images){
						this.$toast(`请上传图片~`)
					}
					if(!item.business){
						this.$toast(`请填写第${index+1}个的包裹的商家~`)
					}
					
				})
				
				let flag = this.orderlist.every(item => item.type!='');
				let flag1 = this.orderlist.every(item => item.images!='');
				let flag2 = this.orderlist.every(item => item.business!='');
				let flag3 = this.myimg.length
				if(flag&&flag1&&flag2&&flag3){
					this.show = true
				}
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
				this.myimg.splice(event.index, 1)
				this.orderlist.map(item=>{
					item.images = this.myimg.join(',')
				})
				console.log(this.orderlist);
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: this.$uploadUrl, // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test',
							token:uni.getStorageSync('token')
						},
						success: (res) => {
							let newres = JSON.parse(res.data)
							
							this.myimg.push(newres.data.url)
							
							this.orderlist.map(item=>{
								item.images = this.myimg.join(',')
							})
							console.log(this.orderlist);
							setTimeout(() => {
								resolve(res.data.data)
							}, 1000)
						}
					});
				})
			},
		}
	}
</script>

<style lang="scss">
page{
	background-color: #f4f4f4;
	.returnbox{
		width: 726rpx;
		margin: 40rpx auto;
		background-color: #fff;
		box-sizing: border-box;
		padding: 30rpx 30rpx 500rpx;
		.inputbox {
			display: flex;
			flex-direction: column;
		
			// .input-placeholder {
			// 	color: #000 !important;
			// }
		}
		.myinputbox {
			margin: 20rpx 0;
			display: flex;
			align-items: center;
			
			::v-deep .uni-section {
				width: 500rpx;
				flex: 1;
				// margin-left: 20rpx;
			}
		
			::v-deep .uni-section .uni-section-header {
				position: relative;
				display: flex;
				flex-direction: row;
				align-items: center;
				padding: 12px 10px;
				font-weight: normal;
				display: none;
			}
		}
	}
	.estimate {
		background-color: #fff;
		bottom: 0;
		position: fixed;
		padding: 40rpx;
		width: 100%;
		box-sizing: border-box;
		.estimatebox{
			justify-content: space-between;
			align-items: center;
			display: flex;
		}
		.estimate_left {
			display: flex;
			flex-direction: column;
		}
	
		.makesure {
			background-color: rgb(50, 94, 230);
			padding: 16rpx 30rpx;
			border-radius: 30rpx;
			color: #fff;
		}
	
		.nomakesure {
			background-color: rgb(198, 198, 198);
			padding: 16rpx 30rpx;
			border-radius: 30rpx;
			color: #fff;
		}
	}
}
.plus{
		position: fixed;
		right: 50rpx;
		top: 80%;
		z-index: 99;
	}
</style>
